import traceback

from django.conf import settings
from rest_framework import status
from rest_framework.exceptions import APIException
from django.utils.translation import gettext_lazy as _


class CustomAPIException(APIException):
    def __init__(self, detail=None, code=None, e=None):
        if e and getattr(settings, "DEBUG"):
            if not detail:
                detail = e.message
            traceback.print_exc()

        super(CustomAPIException, self).__init__(detail, code)


# 500
class ServerError(APIException):
    status_code = status.HTTP_503_SERVICE_UNAVAILABLE
    default_detail = _('服务器内部错误')
    custom_code = '01'


# 404 not found
class Error404(APIException):
    status_code = status.HTTP_404_NOT_FOUND
    default_detail = _('404 NOT FOUND')
    custom_code = '01'


# class ApiStatusError(APIException):
#     """第三方接口成功处理，但返回错误状态"""
#     status_code = status.HTTP_503_SERVICE_UNAVAILABLE
#     default_detail = _('数据获取错误，请检查后重试')
#     default_code = 'api_status_error'

class TaskExistingError(ServerError):
    default_detail = _('Task Already Exist')
    custom_code = '06'


class APIScannerNotOk(ServerError):
    default_detail = _('Scanner API Not Ok')
    custom_code = '02'


class APIScannerCreateTaskFail(ServerError):
    default_detail = _('Scanner API Create Task Fail')
    custom_code = '04'


class APIScannerException(ServerError):
    default_detail = _('Scanner API Exception')
    custom_code = '03'


class NoticeError(ServerError):
    msg = "Accept notice error"
    custom_code = '05'


class TaskNotFoundError(Exception):
    pass


# 403 FORBIDDEN
class Error403(APIException):
    status_code = status.HTTP_403_FORBIDDEN
    default_detail = _('403 FORBIDDEN')
    custom_code = '01'


# 400 BAD REQUEST
class Error400(APIException):
    status_code = status.HTTP_403_FORBIDDEN
    default_detail = _('400 BAD REQUEST')
    custom_code = '01'


class BackRestoreProcessing(Error403):
    default_detail = _('备份进行中，无法进行操作')


class PushNotificationError(Exception):
    pass
